package com.daycomtech.jgooglechart.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import com.daycomtech.jgooglechart.JGChartConstants;
import com.daycomtech.jgooglechart.dataobjects.JGChartAxis;

public class JGChartMakerStringUtil
{



	static public String getChartSize(int width, int height)
	{
		String ret;

		ret = JGChartConstants.ChartSize+"="+width+"x"+height;

		return ret;
	}

	static public String getChartData(float[] data)
	{
		String ret;

		String textEncodedData = JGChartEncode.textEncode(data);

		ret = JGChartConstants.ChartData+"="+JGChartConstants.ChartTextEncoding+":"+textEncodedData;

		return ret;
	}


	static public String getChartDataScaling(float min_value, float max_value)
	{
		String ret="";

		if (max_value != 0 || min_value != 0 )
		{
			ret = JGChartConstants.ChartDataScaling+"="+min_value+","+max_value;
		}
		return ret;
	}


	static public String getChartType(String type)
	{
		String ret;

		ret = JGChartConstants.ChartType+"="+type;

		return ret;

	}


	static public String getChartLabels(String[] labels) throws UnsupportedEncodingException
	{
		String ret="";

		for (int i = 0; i < labels.length; i ++)
		{
			if (i>0)
			{
				ret = ret +"|";
			}

			ret = ret + URLEncoder.encode(labels[i],"UTF-8");
		}

//		ret = URLEncoder.encode(ret,"UTF-8");

		ret = JGChartConstants.ChartLabels+"="+ret;


		return ret;
	}

	static public String getChartColors(String[] colors)
	{
		String ret="";

		for (int i = 0; i < colors.length; i ++)
		{
			if (i>0)
			{
				ret = ret +",";
			}

			ret = ret + colors[i];
		}

		ret = JGChartConstants.ChartColors+"="+ret;

		return ret;
	}


	static public String getChartAxis(ArrayList<JGChartAxis> group_chart_axis)
	{

		if (group_chart_axis==null)
		{
			return "";
		}

		int total_chart_axis = group_chart_axis.size();

		String ret =JGChartConstants.ChartAxisType+"=";

		//Make axis type
		int i = 1;
		for (JGChartAxis chart_axis : group_chart_axis)
		{
			ret = ret + chart_axis.getType();

			if (i<total_chart_axis)
			{
				ret = ret + ",";
			}
			i++;
		}

		//Make axis range
		i=0;
		String axis_range_values="";
		for (JGChartAxis chart_axis : group_chart_axis)
		{
			if (chart_axis.getEndOfRange()>0)
			{
				axis_range_values = axis_range_values +i+",";
				axis_range_values = axis_range_values + chart_axis.getStartOfRange()+",";
				axis_range_values = axis_range_values + chart_axis.getEndOfRange();
			}

			if ((i+1)<total_chart_axis)
			{
				axis_range_values = axis_range_values +"|";
			}
		}
		if (axis_range_values.length()>0)
		{
			ret = ret +"&"+JGChartConstants.ChartAxisRange+"="+axis_range_values;
		}




		return ret;
	}

	static public String getChartBarWidthAndSpacing(int barWidth, int barSpacing)
	{
		String ret = "";

		if (barWidth > 0 )
		{
			ret = JGChartConstants.ChartBarWidthAndSpacing+"=";
			ret = ret + barWidth;

			if (barSpacing>0)
			{
				ret = ret + ",";
				ret = ret + barSpacing;
			}
		}

		return ret;
	}

	static public String getChartGridLines(String gridlines)
	{
		String ret = "";

		if (gridlines.length()>0)
		{
			ret = JGChartConstants.ChartGridLines+"="+gridlines;
		}

		return ret;
	}







	static public String getUrlString(ArrayList<String> urlStringParts)
	{
		String ret = JGChartConstants.GOOGLE_CHART_URL+"?";

		int i = 0;
		for (String urlStringPart : urlStringParts)
		{
			if (i>0)
			{
				//ret = ret +"&amp;";
				ret = ret +"&";
			}

			ret = ret + urlStringPart;

			i++;
		}

		return ret;
	}
}
